package com.toplabs.unashu.config;

import com.toplabs.unashu.aspect.CurrentContentMethodArgumentResolver;
import com.toplabs.unashu.interceptor.ApiSafeInterceptor;
import com.toplabs.unashu.interceptor.AuthInterceptor;
import com.toplabs.unashu.interceptor.CorsInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author toplabs
 * @since 2022/11/21
 * <p>This java file was created by toplabs in 2022/11/21.
 * The following is the description information about this file:</p>
 * <p>description: 网络配置</p>
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final CorsInterceptor corsInterceptor;
    private final ApiSafeInterceptor apiSafeInterceptor;
    private final AuthInterceptor authInterceptor;

    public WebConfig(CorsInterceptor corsInterceptor, ApiSafeInterceptor apiSafeInterceptor, AuthInterceptor authInterceptor) {
        this.corsInterceptor = corsInterceptor;
        this.apiSafeInterceptor = apiSafeInterceptor;
        this.authInterceptor = authInterceptor;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentContentMethodArgumentResolver());
    }

    @Bean
    public CurrentContentMethodArgumentResolver currentContentMethodArgumentResolver() {
        return new CurrentContentMethodArgumentResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 跨域拦截
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        // api安全拦截
        registry.addInterceptor(apiSafeInterceptor).addPathPatterns("/**");
        // api授权拦截
        registry.addInterceptor(authInterceptor).addPathPatterns("/**");
    }
}
